home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
oper_sys
/
emerald
/
emrldsys.lha
/
Language
/
Compiler
/
cliOpNames.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-08-16
|
4KB
|
175 lines
/*
* @(#)cliOpNames.c 1.5 10/24/89
*/
#include "assert.h"
#include "types.h"
#include <errno.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
extern int errno;
#include <netdb.h>
#include <signal.h>
#define SOCKETNUMBER 2019
#include "opNames.h"
extern char *emDirectory;
int serverSocket;
static void send(socket, string)
int socket;
char *string;
{
int length = strlen(string) + 1;
if (write(socket, string, length) != length) {
fprintf(stderr, "Write failed on socket %d\n", socket);
fprintf(stderr, "String was \"%s\"\n", string);
abort();
}
}
static void receive(socket, string)
int socket;
char *string;
{
int length;
length = read(socket, string, 4096);
if (length < 0) {
perror("receive: (read)");
abort();
} else if (length == 0) {
*string = '\0';
}
}
char *opNameServerArgs[] = {
"OpNameServer",
(char *) 0
};
extern int callsys();
int establishConnection()
{
struct sockaddr_in serverAddress;
#define USEHOSTLOOKUP
#ifdef USEHOSTLOOKUP
char *myHostName = EMSERVERHOST;
struct hostent *myHost;
int machineNameGiven = 1;
#endif
char binname[100];
int success = 1, try = 0;
again:
if (++try > 2) {
success = 0;
goto cleanup;
}
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket < 0) {
perror("establishConnection: (socket)");
success = 0;
goto cleanup;
}
#ifdef USEHOSTLOOKUP
if (! machineNameGiven) {
#define SIZEOFMYHOSTNAME 100
if (gethostname(myHostName, SIZEOFMYHOSTNAME) < 0) {
perror("establishConnection: (gethostname)");
success = 0;
goto cleanup;
}
}
myHost = gethostbyname(myHostName);
if (myHost == NULL) {
perror("establishConnection: (gethostbyname)");
success = 0;
goto cleanup;
}
bzero((char *)&serverAddress, sizeof(serverAddress));
bcopy((char *)myHost->h_addr, (char *)&serverAddress.sin_addr,
myHost->h_length);
#else
serverAddress.sin_addr.s_addr = 0x36015f80;
#endif
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = ntohs(SOCKETNUMBER);
if (connect(serverSocket, (struct sockaddr *)&serverAddress,
sizeof(serverAddress)) == -1) {
perror("establishConnection: (connect)");
fprintf(stderr, "Trying to re-start server\n");
sprintf(binname, "%s/bin/startOpNameServer", emDirectory);
callsys(binname, opNameServerArgs, (char *)NULL,
(char *)NULL);
fprintf(stderr, "Started server, trying again.\n");
if (close(serverSocket) < 0) perror("close");
goto again;
}
#ifdef DEBUG
fprintf(stdout, "Connected to %d\n", ntohs(serverAddress.sin_port));
#endif
cleanup:
return(success);
}
void ON_initialize()
{
if (establishConnection() != 1) assert(0);
}
void ON_finalize()
{
if (close(serverSocket) != 0) assert(0);
}
static OID nextOID = 0, maxOID = 0;
char buffer[128];
OID AllocateOID()
{
if (nextOID == 0 || nextOID > maxOID) {
sprintf(buffer, "A \n");
send(serverSocket, buffer);
receive(serverSocket, buffer);
if (sscanf(buffer, "A 0x%x\n", &nextOID) != 1) assert(FALSE);
maxOID = nextOID + OIDBLOCKSIZE - 1;
}
return(nextOID++);
}
OID ON_Translate(s)
char *s;
{
OID result;
sprintf(buffer, "T %s\n", s);
send(serverSocket, buffer);
receive(serverSocket, buffer);
if (sscanf(buffer, "T 0x%x\n", &result) != 1) assert(FALSE);
return(result);
}
char result[128];
char *ON_Name(id)
OID id;
{
sprintf(buffer, "N 0x%08x\n", id);
send(serverSocket, buffer);
receive(serverSocket, buffer);
/* this one returns null since the answer can be "N \n" */
if (sscanf(buffer, "N %s\n", result) != 1) return(NULL);
return(result);
}